/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.service.impl;

import cz.muni.fi.pa165.machineryrental.converters.MachineDTOConverter;
import cz.muni.fi.pa165.machineryrental.converters.RevisionDTOConverter;
import cz.muni.fi.pa165.machineryrental.dao.MachineDAO;
import cz.muni.fi.pa165.machineryrental.dto.MachineDTO;
import cz.muni.fi.pa165.machineryrental.entities.Machine;
import cz.muni.fi.pa165.machineryrental.entities.Revision;
import cz.muni.fi.pa165.machineryrental.service.MachineService;
import org.springframework.stereotype.Service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of MachineService
 *
 * @author Marian Lorenc, 374122
 */
@Service("MachineService")
@Component
public class MachineServiceImpl implements MachineService {

    @Autowired
    private MachineDAO machineDAO;
    
    public void setMachineDAO(MachineDAO machineDAO) {
        if (machineDAO == null) {
            throw new IllegalArgumentException("Given MachineDAO cannot be null.");
        }

        this.machineDAO = machineDAO;
    }

    @Transactional(readOnly = true)
    public MachineDTO getById(long id) {

        Machine machine = machineDAO.getById(id);

        if (machine == null) {
            return null;
        }
        return MachineDTOConverter.toDTO(machine);
    }

    @Transactional
    public void create(MachineDTO machine) {
        if (machine == null) {
            throw new IllegalArgumentException("Given MachineDAO cannot be null.");
        }
        Machine entity = MachineDTOConverter.toEntity(machine);
        this.machineDAO.create(entity);
        MachineDTOConverter.applyToDTO(entity, machine);
    }

    @Transactional
    public void update(MachineDTO machine) {
        if (machine == null) {
            throw new IllegalArgumentException("Given MachineDAO cannot be null.");
        }

        this.machineDAO.update(MachineDTOConverter.toEntity(machine));
    }

    @Transactional
    public void remove(MachineDTO machine) {
        if (machine == null) {
            throw new IllegalArgumentException("Given MachineDAO cannot be null.");
        }

        machineDAO.remove(MachineDTOConverter.toEntity(machine));
    }

    @Transactional(readOnly = true)
    public List<MachineDTO> findAll() {
        return MachineDTOConverter.toDTOList(this.machineDAO.findAll());
    }

    @Transactional(readOnly = true)
    public MachineDTO findByCode(String code) {
        if (code == null) {
            throw new IllegalArgumentException("Given code cannot be null.");
        }
        Machine machine = machineDAO.findByCode(code);

        if (machine == null) {
            return null;
        }

        return MachineDTOConverter.toDTO(machine);
    }

    @Transactional(readOnly = true)
    public List<MachineDTO> findByBrand(String brand) {
        if (brand == null) {
            throw new IllegalArgumentException("Given brand cannot be null.");
        }
        return MachineDTOConverter.toDTOList(this.machineDAO.findByBrand(brand));
    }

    @Transactional(readOnly = true)
    public List<MachineDTO> findByModel(String model) {
        if (model == null) {
            throw new IllegalArgumentException("Given model cannot be null.");
        }
        return MachineDTOConverter.toDTOList(this.machineDAO.findByModel(model));
    }

    @Transactional(readOnly = true)
    public boolean isCodeUnique(String code) {
        return this.machineDAO.isCodeUnique(code);
    }
}
